<template>
 <Modal
  :title="title"
  @on-cancel="onClickCancel"
  v-model="isShow">
  <Form :label-width="100" :rules="rules" ref="form" :model="form">
    <FormItem label="选择经销商" class="wp80" prop="distributorId" v-if="isAdd">
      <Select
        filterable
        remote
        :remote-method="getStoreData"
        clearable
        @on-query-change="test"
        :loading="loading"
        v-model.trim="form.distributorId"
        :label-in-value="true"  >
        <Option v-for="item in list" :value="item.id" :key="item.id">{{item.name | filterStore}}</Option>
      </Select>
    </FormItem>
    <FormItem label="选择经销商" class="wp80" v-else>
       <Input v-model="distributorName" :disabled="true" />
    </FormItem>
    <FormItem label="限购数量" class="wp80" prop="num">
      <InputNumber class="wp100" v-model="form.num" :min="1" :max="9999" ></InputNumber>
    </FormItem>
  </Form>
  <Loader :status="status" @refresh="getData"></Loader>
  <div slot="footer">
    <Button size="large"  @click="onClickCancel">取消</Button>
    <Button size="large" type="primary" @click="onClickOk">确定</Button>
  </div>
 </Modal>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      id: this.$route.query.id,
      isShow: false,
      status: 'pass',
      title: '新增经销商限购',
      list: [],
      itemId: '',
      loading: false,
      isAdd: true,
      distributorName: '',
      form: {
        distributorId: '',
        num: ''
      },
      rules:{
        distributorId: [{ required: true, message: '请选择经销商', trigger: 'change' }],
        num: [{required: true, message: '请设置限购数量', type: 'number', trigger: 'change'}]
      }
    }
  },
  mounted() {
    // this.$store.state.bargain.form.isStoreSet ="1" ? this.getStoreData() : ''
  },
  methods: {
    test(val) {
      if (!val) {
        this.getStoreData()
      }
    },
    setShowMotal(row, index) {
      if (row !== 1) { // 新增
        this.isAdd = false
        this.distributorName = row.item.distributorName
        this.form.distributorId = row.item.distributorId
        this.form.num = row.item.limitNum
        this.itemId = row.item.id
        this.title ='修改经销商限购'
      } else {
        this.form.distributorId = ''
        this.form.num = ''
        this.isAdd = true
        this.itemId = ''
        this.title = '新增经销商限购'
        if (!this.list.length) this.getStoreData()
      }
      // this.isShow = true
    },
    getStoreData(id) {
      this.loading = true
      this.status = 'loading'
      let param = {
        current: '1',
        size: '20',
        name: id,
        activityId: this.$route.query.id
      }
      this.$store.dispatch('getDistributorList', param).then(res => {
        this.loading = false
        this.list = (res.records || []).map(item => {
          return {
            id: item.distributorId,
            name: `${item.distributorName} ${item.distributorContact ? item.distributorContact: ''}：${item.distributorMobile ? item.distributorMobile : ''}`,
            contactName: item.distributorContractName,
            contactMobile: item.distributorMobile
          }
        })
        this.status = 'pass'
      })
    },
    onClickOk() {
      let params = {
        activityId: this.$route.query.id,
        distributorId: this.form.distributorId,
        limitNum: this.form.num
      }
      let url = '/activity/factory_info/exceptional_distributor/save'
      if (this.itemId) {
        params.id = this.itemId
        url = '/activity/factory_info/exceptional_distributor/edit'
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          this.status = 'loading'
          this.$jsonPost(url, params).then(res => {
            this.$emit('complete')
            this.isShow = false
            this.status = 'pass'
          }).catch(error => {
            this.status = 'pass'
          })
        } else {
          this.isShow = true
        }
      })
    },
    onClickCancel() {
      this.isShow = false
      try {
        this.$refs['form'].resetFields();
      } catch(res) {
      }
    },
    onChangeDenomination() {

    }
  },
  filters: {
    filterStore(val) {
      return val
    }
  },
  computed: {
   ...mapGetters([
     'exceptionalStoreDTOList'
   ]) 
  }
}
</script>